﻿import QtQuick 2.0
import QtQuick.Window 2.2

//贪吃蛇
Item {
    id: snake
    anchors.fill: parent

    property var items: new Array
    property bool isStartOnce: false
    property real currentSendAngle: 0
    property real angleDt: 0    //角度变化差

    property real lastPointCenterX: 0     //前一个点坐标
    property real lastPointCenterY: 0     //前一个点坐标
    property real lastAngle: 0

    property real lastPointRCenterX: 0     //前一个点坐标R
    property real lastPointRCenterY: 0     //前一个点坐标R

    property int ballRadius: 50

    property alias head: head

    property bool isEat: false

    property bool bianjie: false

    property alias rotationHead: rotationHead


    function setHeadAngle(angle)
    {
        rotationHead.angle = angle
    }

    function stopTouch(){
        isStartOnce = false
        rotationHead.angle = rotationHead.angle % 360
    }

    function move(step)
    {
        var radian = Math.PI/180 * rotationHead.angle
        var x = Math.cos(radian) * step
        var y = Math.sin(radian) * step


        var headCenterX = head.x + ballRadius
        var headCenterY = head.y + ballRadius

        //头原坐标
        lastPointCenterX = headCenterX
        lastPointCenterY = headCenterY
        lastAngle = rotationHead.angle

//        if(isEat)
//            console.log("---", isEat, lastPointCenterX, lastPointCenterY, x, y)

        var point = checkScreen(lastPointCenterX+x, lastPointCenterY+y)

        head.x = point.x-ballRadius

        head.y = point.y-ballRadius

        for(var i=0; i<items.length; i++){
            var tx = items[i].x + ballRadius
            var ty = items[i].y + ballRadius

            var bodyPoint = Qt.point(lastPointCenterX, lastPointCenterY)

            items[i].x = bodyPoint.x - ballRadius
            items[i].y = bodyPoint.y - ballRadius

            lastPointCenterX = tx
            lastPointCenterY = ty
        }
    }

    function eat(type)
    {
        isEat = true
        var obj = Qt.createComponent("Body.qml").createObject(snake)
        obj.source = "qrc:/img/img/ball.png"
        var radian

        if(items.length==0){
            obj.x = head.x
            obj.y = head.y
            obj.z = head.z-1
        }
        else{
            obj.x = items[items.length-1].x
            obj.y = items[items.length-1].y
            obj.z = items[items.length-1].z-1
        }

        obj.isShunYi = false

        items.push(obj)

        move(step)

        isEat = false

    }

    //超出屏幕，又从另一端回来
    function getOutScreen(x, y, obj)
    {
        var w = Screen.width
        var h = Screen.height
//        console.log("-c----------", w, h, x, y)
        var a = 0

        var point = Qt.point(x,y)

        if(x+ballRadius*2<0){
            point.x = w+ballRadius*2
            a++
        }
        else if(x-ballRadius*2>w){
            point.x = 0-ballRadius*2
            a++
        }

        if(y+ballRadius*2<0){
            point.y = h+ballRadius*2
            a++
        }
        else if(y-ballRadius*2>h){
            point.y = 0-ballRadius*2
            a++
        }

        if(a>0){
            obj.isShunYi = true
        }
        else{
            obj.isShunYi = false
        }

        console.log("=======", obj.isShunYi)

        return point
    }


    function checkScreen(x, y){
        var w = Screen.width
        var h = Screen.height

        var point = Qt.point(x,y)


        if(x<=0 && (rotationHead.angle>=90 && rotationHead.angle<=180)){
            rotationHead.angle = 180-rotationHead.angle
            minusScore()
        }
        else if(x<=0 && (rotationHead.angle>=-180 && rotationHead.angle<=-90)){
            rotationHead.angle = -180-rotationHead.angle
            minusScore()
        }
        else if(x>=w && (rotationHead.angle>=-90 && rotationHead.angle<=0)){
            rotationHead.angle = -180-rotationHead.angle
            minusScore()
        }
        else if(x>=w && (rotationHead.angle>=0 && rotationHead.angle<=90)){
            rotationHead.angle = 180-rotationHead.angle
            minusScore()
        }

        if(y<=0 && (rotationHead.angle>=-180 && rotationHead.angle<=-90) ){
            rotationHead.angle = -rotationHead.angle
            minusScore()
        }
        else if(y<=0 && (rotationHead.angle>=-90 && rotationHead.angle<=0) ){
            rotationHead.angle = -rotationHead.angle
            minusScore()
        }
        else if(y>=h && (rotationHead.angle>=0 && rotationHead.angle<=90) ){
            rotationHead.angle = -rotationHead.angle
            minusScore()
        }
        else if(y>=h && (rotationHead.angle>=90 && rotationHead.angle<=180) ){
            rotationHead.angle = -rotationHead.angle
            minusScore()
        }


        return point
    }

    Image{
        id: head
        source: "qrc:/img/img/head.png"
        z: 2
        y: (parent.height-height)/2
        x: (parent.width-width)/2

        transform: Rotation{
            id: rotationHead
            axis.x: 0
            axis.y: 0
            axis.z: 1
            origin.x: head.width/2
            origin.y: head.height/2
        }

        property bool isShunYi: false

        Behavior on x{
            NumberAnimation{duration: head.isShunYi?0:timerGame.interval}
        }
        Behavior on y{
            NumberAnimation{duration: head.isShunYi?0:timerGame.interval}
        }
    }
}

